package com.oracle2mysql.migration;

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;

/**
 * HTML差异生成器测试工具类，用于验证修复后的HTML报告功能
 */
public class HtmlDiffGeneratorTestUtil {

    private static final Logger logger = LoggerFactory.getLogger(HtmlDiffGeneratorTestUtil.class);

    /**
     * 测试HTML报告生成功能，包括左右布局和点击功能
     */
    @Test
    public void testHtmlReportGeneration() throws IOException {
        // 创建临时测试目录
        File testDir = new File("target/test-html-report");
        if (!testDir.exists()) {
            testDir.mkdirs();
        }
        
        // 创建测试文件
        File oracleFile = new File(testDir, "oracle-sample.sql");
        File mysqlFile = new File(testDir, "mysql-sample.sql");
        
        // 写入Oracle SQL样例，包含include标签和行内差异
        String oracleSql = "-- Oracle SQL示例\n" +
                "SELECT id, name, created_date, <include refid=\"Base_Column_List\"/>\n" +
                "FROM users\n" +
                "WHERE status = 'ACTIVE'\n" +
                "ORDER BY created_date DESC;";
        Files.write(Paths.get(oracleFile.getPath()), oracleSql.getBytes(StandardCharsets.UTF_8));
        
        // 写入MySQL SQL样例，包含include标签和行内差异
        String mysqlSql = "-- MySQL SQL示例\n" +
                "SELECT id, name, created_at AS created_date, <include refid=\"Base_Column_List\"/>, email\n" +
                "FROM users\n" +
                "WHERE status = 'ACTIVE' AND age > 18\n" +
                "ORDER BY created_at DESC;";
        Files.write(Paths.get(mysqlFile.getPath()), mysqlSql.getBytes(StandardCharsets.UTF_8));
        
        // 创建文件对 - 使用HtmlDiffGenerator.FilePair类
        List<HtmlDiffGenerator.FilePair> filePairs = new ArrayList<>();
        filePairs.add(new HtmlDiffGenerator.FilePair(oracleFile, mysqlFile));
        
        // 生成HTML报告
        String outputHtmlPath = testDir.getPath() + File.separator + "test-diff-report.html";
        HtmlDiffGenerator generator = new HtmlDiffGenerator();
        generator.generateDiffReport(filePairs, outputHtmlPath);
        
        logger.info("测试HTML报告已生成: {}", outputHtmlPath);
        logger.info("请在浏览器中打开此文件，验证左右布局和点击功能是否正常工作");
        
        // 验证文件是否生成
        File outputFile = new File(outputHtmlPath);
        assert outputFile.exists() : "HTML报告文件未生成";
        assert outputFile.length() > 0 : "HTML报告文件为空";
        
        // 验证HTML内容包含预期的CSS类和结构
        String htmlContent = new String(Files.readAllBytes(Paths.get(outputHtmlPath)));
        assert htmlContent.contains("diff-container") : "HTML不包含diff-container类";
        assert htmlContent.contains("diff-content") : "HTML不包含diff-content类";
        assert htmlContent.contains("\"tab-button active\" onclick=") : "HTML中tab-button的属性不正确";
        
        logger.info("HTML报告验证通过！");
    }
}